package com.qf.xiaomi.utils;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

/**
 * 使用Druid创建连接池--已正常
 */

public class DruidDButils {
    private static Properties properties=null;
    private static  Class<?> classes=null;
    private static  DruidDataSource druidDataSource=null;

    static{
       // System.out.println("正在载入配置文件……");
        InputStream resourceAsStream = DruidDButils.class.getResourceAsStream("/com/qf/xiaomi/resources/database.properties");
        if(resourceAsStream ==null){
            System.out.println("配置文件载入失败,请检查配置文件路径是否有误！！！");
        }else {
            properties=new Properties();
            //System.out.println("配置文件载入成功！开始装载配置文件……");
            try {
                properties.load(resourceAsStream);
                /*System.out.println("配置文件装载成功！");
                System.out.println(properties);//输出配置文件信息*/
                //根据配置文件获取数据库对象
                druidDataSource= (DruidDataSource) DruidDataSourceFactory.createDataSource(properties);
            } catch (IOException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static Connection getDruidConn(){///获取数据库连接
        if(druidDataSource!=null){
            try {

              return   druidDataSource.getConnection();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return  null;
    }


}
